package com.ls.uav_backend.manger;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.ls.uav_backend.model.vo.AmapWeatherResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.beans.factory.annotation.Value;

@Component
@Slf4j
public class WeatherManger {

    @Value("${gaode.api-key}")
    private String apikey;

    @Value("${gaode.adcode}")
    private String adcode;

    public AmapWeatherResponse getCurrentWeather() {
        try {
            String requestPath = String.format("https://restapi.amap.com/v3/weather/weatherInfo?city=%s&key=%s", adcode, apikey);
            String result = HttpUtil.get(requestPath);
            return JSONUtil.toBean(result, AmapWeatherResponse.class);
        } catch (Exception e) {
            log.error("获取天气失败，{}", e.getMessage());
            return null;
        }
    }
}
